package com.aqie.meet.ui.find;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aqie.libcommon.adapter.CommonAdapter;
import com.aqie.libcommon.adapter.CommonViewHolder;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libcommon.app.ToolbarActivity;
import com.aqie.libcommon.utils.SimpleJsonTool;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.api.post.Comment;
import com.aqie.libfactory.model.api.post.Comment2;
import com.aqie.libfactory.model.api.user.UserPost;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.interfaces.OnPraiseOrCommentClickListener;
import com.aqie.meet.ui.login.AccountActivity;
import com.aqie.meet.widget.EmojiPanelView;
import com.bumptech.glide.Glide;
import com.google.android.material.button.MaterialButton;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostDetailActivity extends ToolbarActivity implements OnPraiseOrCommentClickListener {

    private String postId;

    @BindView(R.id.post_text)
    TextView post_text;

    @BindView(R.id.post_images)
    ImageView post_images;

    @BindView(R.id.post_time)
    TextView post_time;

    @BindView(R.id.comment_num)
    MaterialButton comment_num;

    @BindView(R.id.emoji_panel_view)
    EmojiPanelView mEmojiPanelView;

    @BindView(R.id.edit_text)
    EditText edit_text;

    @BindView(R.id.posts)
    ConstraintLayout mPost;

    // 评论
    @BindView(R.id.recycler_view)
    RecyclerView mRecycleView;
    private List<Comment2> mList = new ArrayList<>();
    private CommonAdapter<Comment2> mAdapter;
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_post_detail;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle("动态详情");
        Intent intent = getIntent();
        postId = intent.getStringExtra("postId");
        Log.e(TAG, postId);

        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        // mRecycleView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<Comment2>() {
            @Override
            public void onBindViewHolder(Comment2 model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.comment_content, model.getReply());
                viewHolder.setText(R.id.comment_author, model.getUsername());
                viewHolder.setText(R.id.comment_time, model.getCreateTime());
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.post_comment_item;
            }
        });
        mRecycleView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getPostDetail(postId);
        //getPostComments(postId);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPostComments(postId);
    }

    private void getPostComments(String postId) {
        RemoteService service = Network.remote();
        Call<RspModel<List<Comment2>>> call = service.commentList(postId);
        call.enqueue(new Callback<RspModel<List<Comment2>>>() {
            @Override
            public void onResponse(Call<RspModel<List<Comment2>>> call, Response<RspModel<List<Comment2>>> response) {
                RspModel<List<Comment2>> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    // BaseApplication.showToast("获取评论成功");
                    Log.e(TAG, "获取评论成功");
                    String rspString = SimpleJsonTool.toJson(body.getData()); Log.e(TAG, rspString);
                    List<Comment2> comments = body.getData();
                    if(comments.size() == 0) {
                        mRecycleView.setVisibility(View.GONE);
                    } else {
                        mRecycleView.setVisibility(View.VISIBLE);

                        if (mList.size() > 0) {
                            mList.clear();
                        }
                        mList.addAll(comments);
                        mAdapter.notifyDataSetChanged();
                        Log.e(TAG, "my list:" + mList.toString());
                    }
                }else {
                    BaseApplication.showToast("获取评论失败");
                    Log.e(TAG, "获取评论失败" + body.getMsg());
                }
            }

            @Override
            public void onFailure(Call<RspModel<List<Comment2>>> call, Throwable t) {
                BaseApplication.showToast("获取评论失败");
                Log.e(TAG, "获取评论失败" + t.getMessage());
            }
        });
    }

    private void getPostDetail(String postId) {
        RemoteService service = Network.remote();
        Call<RspModel<UserPost>> call = service.postDetail(postId);
        call.enqueue(new Callback<RspModel<UserPost>>() {
            @Override
            public void onResponse(Call<RspModel<UserPost>> call, Response<RspModel<UserPost>> response) {
                RspModel<UserPost> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    UserPost data = body.getData();
                    // String rspString = SimpleJsonTool.toJson(data); Log.e(TAG, rspString);
                    // 填充详情到页面
                    post_text.setText(data.getContent());
                    if(data.getPictureList() != null && data.getPictureList().size() > 0){
                        Glide.with(getApplication()).load(data.getPictureList().get(0)).into(post_images);
                    } else {
                        post_images.setVisibility(View.GONE);
                    }
                    /*post_time.setText(data.getPublishTime());
                    comment_num.setText(data.getCommentCount()+"");*/

                }
            }

            @Override
            public void onFailure(Call<RspModel<UserPost>> call, Throwable t) {

            }
        });
    }

    private void publishComment(String commentContent){
        Comment comment = new Comment();
        comment.setReply(commentContent);
        comment.setPid(postId);
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.commentNew(comment);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    // BaseApplication.showToast("发送评论成功");
                    Log.e(TAG, "发送评论成功");
                    getPostComments(postId);
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });
    }

    @OnClick(R.id.posts)
    void hideCommentDialog(){
        mEmojiPanelView.dismiss();
    }

    @OnClick(R.id.img_click_praise_or_comment)
    void showCommentDialog(){
        mEmojiPanelView.showEmojiPanel();
    }

    @OnClick(R.id.tv_send_comment)
    void sendComment(){
        String commentContent = edit_text.getText().toString();
        mEmojiPanelView.dismiss();
        publishComment(commentContent);
        edit_text.setText("");
    }

    @Override
    public void onPraiseClick(int position) {

    }

    @Override
    public void onCommentClick(int position) {
        mEmojiPanelView.showEmojiPanel();
    }
}